import { addExtra } from 'puppeteer-extra'
import { funcFns } from '@/puppeteer/utils/funtion'
import puppeteerCore from 'puppeteer-core'
import StealthPlugin from 'puppeteer-extra-plugin-stealth'
import AnonymizeUserAgent from 'puppeteer-extra-plugin-anonymize-ua'

// import AcceptLanguagePlugin from 'puppeteer-extra-plugin-stealth/evasions/accept-language';
// import ChromeRuntimePlugin from 'puppeteer-extra-plugin-stealth/evasions/chrome.runtime';
// import ConsoleDebugPlugin from 'puppeteer-extra-plugin-stealth/evasions/console.debug';
// import IFrameContentWindowPlugin from 'puppeteer-extra-plugin-stealth/evasions/iframe.contentWindow';
// import MediaCodecsPlugin from 'puppeteer-extra-plugin-stealth/evasions/media.codecs';
// import NavigatorLanguagesPlugin from 'puppeteer-extra-plugin-stealth/evasions/navigator.languages';
import NavigatorPermissionsPlugin from 'puppeteer-extra-plugin-stealth/evasions/navigator.permissions'
import NavigatorPlugins from 'puppeteer-extra-plugin-stealth/evasions/navigator.plugins'
// import WebdriverPlugin from 'puppeteer-extra-plugin-stealth/evasions/navigator.webdriver';
// import UserAgentPlugin from 'puppeteer-extra-plugin-stealth/evasions/user-agent';
// import WebglVendorPlugin from 'puppeteer-extra-plugin-stealth/evasions/webgl.vendor';
// import WindowOuterDimensionsPlugin from 'puppeteer-extra-plugin-stealth/evasions/window.outerdimensions';

async function initialWindow() {
  const plugins = [
    StealthPlugin(),
    // ChromeRuntimePlugin(),
    // IFrameContentWindowPlugin(),
    // MediaCodecsPlugin(),
    // NavigatorLanguagesPlugin(),
    NavigatorPermissionsPlugin(),
    NavigatorPlugins(),
    // WebdriverPlugin(),
    // WebglVendorPlugin(),
    // WindowOuterDimensionsPlugin(),
    // UserAgentPlugin(),
    // AcceptLanguagePlugin(),
    // ConsoleDebugPlugin(),
    AnonymizeUserAgent(),
  ]
  //let winSizeArg = "--window-size=${width},${winHeight}"
  const puppeteer = addExtra(puppeteerCore)
  const browser = await puppeteer.launch({
    executablePath: funcFns.chromePath(),
    headless: false,
    defaultViewport: null,
    ignoreDefaultArgs: ['--enable-automation', '--disable-extensions'],
    args: [
      '--start-maximized',
      '--disable-notifications=true',
      '--unhandled-rejections=strict',
      '--disable-gpu',
      '--disable-dev-shm-usage',
      '--disable-setuid-sandbox',
      '--no-first-run',
      '--no-sandbox',
      '--no-zygote',
    ],
    slowMo: 10,
  })
  for (const plugin of plugins) {
    await plugin.onBrowser(browser)
  }
  const page = await browser.newPage()
  for (const plugin of plugins) {
    await plugin.onPageCreated(page)
  }
  // await page.setViewport({ width: width, height: winHeight });
  await page.setUserAgent(
    'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36'
  )
  return { window: browser, page: page }
}
export { initialWindow }
